﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Printing;
using eService;
using eService.BLL;
using System.Configuration;
using app.Common;
using System.Xml;

namespace eService.Presentation
{
    public partial class usrcCauHinh_HoaDon : UserControl
    {
        private XmlDocument xmlDoc = new XmlDocument();
        ExcuteCAUHINH exe = new ExcuteCAUHINH();
        public usrcCauHinh_HoaDon()
        {
            InitializeComponent();
            LoadThongSo();
            getListPrinter();
        }
        private void getListPrinter()
        {
            try
            {
                string BillPrinter="",ReportPrinter="";
                BillPrinter = AppSetting.getSetting().getConfigValue("BillPrinter");
                ReportPrinter = AppSetting.getSetting().getConfigValue("ReportPrinter");
                this.cmbMayInBC.BindingContext = new BindingContext();
                this.cmbMayInHD.BindingContext = new BindingContext();
                foreach (string text in PrinterSettings.InstalledPrinters)
                {
                    this.cmbMayInHD.Items.Add(new ItemCombobox(text.ToString(), text.ToString()));
                    this.cmbMayInBC.Items.Add(new ItemCombobox(text.ToString(), text.ToString()));
                }

                exe.SelectValueCombobox(this.cmbMayInHD, BillPrinter);
                exe.SelectValueCombobox(this.cmbMayInBC, ReportPrinter);
            }
            catch
            {
            }
        }
        private void LoadThongSo()
        {
            try
            {
                string SoInHoaDon = "", CauCuoiHD = "", HienTienKM = "", HienTenNguoiLap = "", HoaDonThuGon = "", XemTruocHD = "", MayinHoaDon = "", MayInBaoCao = "", BillShort = "",Print="";
                SoInHoaDon = AppSetting.getSetting().getConfigValue("Number");
                CauCuoiHD = AppSetting.getSetting().getConfigValue("Footer");
                HienTienKM = AppSetting.getSetting().getConfigValue("ShowPromote");
                HienTenNguoiLap = AppSetting.getSetting().getConfigValue("UserCreate");
                HoaDonThuGon = AppSetting.getSetting().getConfigValue("BillShort");
                XemTruocHD = AppSetting.getSetting().getConfigValue("ViewAndPrintBill");
                MayinHoaDon = AppSetting.getSetting().getConfigValue("BillPrinter");
                MayInBaoCao = AppSetting.getSetting().getConfigValue("ReportPrinter");
                Print = AppSetting.getSetting().getConfigValue("Print");

                if (SoInHoaDon != "" && CauCuoiHD != "" && MayinHoaDon != "" && MayInBaoCao != "")
                {
                    numSoInHoaDon.Value = int.Parse(SoInHoaDon);
                    txtCauCuoiHD.Text = CauCuoiHD;
                    chbHienTienKM.Checked = bool.Parse(HienTienKM);
                    chbHienTenNguoiLap.Checked = bool.Parse(HienTenNguoiLap);
                    chbHoaDonThuGon.Checked = bool.Parse(HoaDonThuGon);
                    chbXemTruocHD.Checked = bool.Parse(XemTruocHD);
                    cmbMayInHD.Text= MayinHoaDon;
                    cmbMayInBC.Text = MayInBaoCao;

                    if (Print == "5") radGiayA5.Checked = true;
                    else radGiayCuon.Checked = true;
                    cmbLoaiGiayCuon.SelectedIndex = exe.getBillScrollType()-1;

                    bool KQ = false;
                    KQ = ConnectionProvider.LoadAllConnection();
                    if (!KQ) MessageBox.Show("Thông tin cấu hình không chính xác!", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
                else
                {
                    MessageBox.Show("Vui lòng điền đầy đủ thông tin!", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
            }
            catch
            {
            }
        }
        private void btnXacNhan_Click(object sender, EventArgs e)
        {
            try
            {
                string SoInHoaDon = "", CauCuoiHD = "", HienTienKM = "", HienTenNguoiLap = "", HoaDonThuGon = "", XemTruocHD = "", MayinHoaDon = "", MayInBaoCao = "", BillShort = "", Print="";
                SoInHoaDon = numSoInHoaDon.Value.ToString();
                CauCuoiHD = txtCauCuoiHD.Text;
                HienTienKM = chbHienTienKM.Checked.ToString();
                HienTenNguoiLap = chbHienTenNguoiLap.Checked.ToString();
                HoaDonThuGon = chbHoaDonThuGon.Checked.ToString();
                XemTruocHD = chbXemTruocHD.Checked.ToString();
                MayinHoaDon = cmbMayInHD.Text.Trim();
                MayInBaoCao = cmbMayInBC.Text.Trim();
                BillShort = chbHoaDonThuGon.Checked.ToString();
                int LoaiGiayCuon = cmbLoaiGiayCuon.SelectedIndex + 1;

                if (radGiayA5.Checked)Print = "5";
                else Print = "7";

                if (SoInHoaDon != "" && CauCuoiHD != "" && MayinHoaDon != "" && MayInBaoCao != "")
                {
                    AppSetting.getSetting().setConfigValue("Number", SoInHoaDon);
                    AppSetting.getSetting().setConfigValue("Footer", CauCuoiHD);
                    AppSetting.getSetting().setConfigValue("ShowPromote", HienTienKM);
                    AppSetting.getSetting().setConfigValue("UserCreate", HienTenNguoiLap);
                    AppSetting.getSetting().setConfigValue("BillShort", HoaDonThuGon);
                    AppSetting.getSetting().setConfigValue("ViewAndPrintBill", XemTruocHD);
                    AppSetting.getSetting().setConfigValue("BillPrinter", MayinHoaDon);
                    AppSetting.getSetting().setConfigValue("ReportPrinter", MayInBaoCao);
                    AppSetting.getSetting().setConfigValue("Print", MayInBaoCao);

                    UpdateKey("Number", SoInHoaDon);
                    UpdateKey("Footer", CauCuoiHD);
                    UpdateKey("ShowPromote", HienTienKM);
                    UpdateKey("UserCreate", HienTenNguoiLap);
                    UpdateKey("BillShort", HoaDonThuGon);
                    UpdateKey("ViewAndPrintBill", XemTruocHD);
                    UpdateKey("BillPrinter", MayinHoaDon);
                    UpdateKey("ReportPrinter", MayInBaoCao);
                    UpdateKey("Print", Print);

                }
                else
                {
                    MessageBox.Show("Vui lòng điền đầy đủ thông tin!", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }

                ConfigurationManager.RefreshSection("appSettings");
                exe.setBillScrollType(LoaiGiayCuon);
                try
                {
                    clsGeneral.BillShort = bool.Parse(BillShort);
                }
                catch
                {
                    clsGeneral.BillShort = false;
                }
                ConfigurationManager.RefreshSection("appSettings");
                MessageBox.Show("Thông tin đã được cập nhật vào hệ thống", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        public void UpdateKey(string strKey, string newValue)
        {
            this.xmlDoc.Load(AppDomain.CurrentDomain.BaseDirectory + "eService.Presentation.exe.config");
            if (!this.KeyExists(strKey))
            {
                this.CreateKey(strKey);
            }
            XmlNode xmlNode = this.xmlDoc.SelectSingleNode("configuration/appSettings");
            foreach (XmlNode xmlNode2 in xmlNode)
            {
                if (xmlNode2.Attributes["key"].Value == strKey)
                {
                    xmlNode2.Attributes["value"].Value = newValue;
                }
            }
            this.xmlDoc.Save(AppDomain.CurrentDomain.BaseDirectory + "eService.Presentation.exe.config");
            this.xmlDoc.Save(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
        }
        public bool KeyExists(string strKey)
        {
            this.xmlDoc.Load(AppDomain.CurrentDomain.BaseDirectory + "eService.Presentation.exe.config");
            XmlNode xmlNode = this.xmlDoc.SelectSingleNode("configuration/appSettings");
            bool result;
            foreach (XmlNode xmlNode2 in xmlNode)
            {
                if (xmlNode2.Attributes["key"].Value == strKey)
                {
                    result = true;
                    return result;
                }
            }
            result = false;
            return result;
        }
        private void CreateKey(string strKey)
        {
            try
            {
                this.xmlDoc.Load(AppDomain.CurrentDomain.BaseDirectory + "eService.Presentation.exe.config");
                XmlNode xmlNode = this.xmlDoc.SelectSingleNode("configuration/appSettings");
                XmlNode xmlNode2;
                if (xmlNode.FirstChild != null)
                {
                    xmlNode2 = xmlNode.FirstChild.Clone();
                }
                else
                {
                    xmlNode2 = this.xmlDoc.CreateElement("add");
                    XmlAttribute node = this.xmlDoc.CreateAttribute("key");
                    XmlAttribute node2 = this.xmlDoc.CreateAttribute("value");
                    xmlNode2.Attributes.Append(node);
                    xmlNode2.Attributes.Append(node2);
                    xmlNode.AppendChild(xmlNode2);
                }
                xmlNode2.Attributes["key"].Value = strKey;
                xmlNode2.Attributes["value"].Value = "";
                xmlNode.AppendChild(xmlNode2);
                this.xmlDoc.Save(AppDomain.CurrentDomain.BaseDirectory + "eService.Presentation.exe.config");
            }
            catch
            {
            }
        }

    }
}
